package com.fengchao.api.controller.v1;

import com.alibaba.fastjson.JSON;
import com.fengchao.api.service.BciService;
import com.fengchao.api.service.CfiService;
import com.fengchao.api.service.UsrService;
import com.fengchao.model.entity.comm.CustomBizException;
import com.fengchao.model.entity.comm.Message;
import com.fengchao.model.entity.comm.ResponseParameter;
import com.fengchao.util.common.CommonValidator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fengchao.api.config.Constant.GET_SUCCESS;
import static com.fengchao.api.config.Constant.PARAMETER_IS_NOT_COMPLETE;

/**
 * Created by wangnian on 2016/7/12.
 */
@RestController
@RequestMapping("/v1/home")
public class HomeController  {

    @Autowired
    private CfiService cfiService;
    @Autowired
    private UsrService usrService;
    @Autowired
    private BciService bciService;

    private static Logger logger = LoggerFactory.getLogger(HomeController.class);

    @RequestMapping(value = "/getHomeCrowdfunding", method = RequestMethod.GET)
    @ApiOperation(value = "(APP)获取首页的众筹最热最新即将", notes = "返回参数: </br>成功:</br>{status: SUCCESS, </br>message: 获取成功, </br>data:{</br>&nbsp;&nbsp;info:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;city: 上海,</br>&nbsp;&nbsp;&nbsp;&nbsp;nameDisplay: 张靓颖(艺人展示名)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;artistId: 1(艺人ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;artistStyle: [{id: 1, typeName: 流行}] 艺人演唱风格</br>&nbsp;&nbsp;&nbsp;&nbsp;nameEn: Jane Zhang(艺人英文名),</br>&nbsp;&nbsp;&nbsp;&nbsp;namePy: ZhangLiangYing(艺人拼音名称)," +
            " </br>&nbsp;&nbsp;&nbsp;&nbsp;title: aaa(众筹标题), </br>&nbsp;&nbsp;&nbsp;&nbsp;sharedUrl: http://aaa.png (分享链接), </br>&nbsp;&nbsp;&nbsp;&nbsp;appIcon: http://aaa.png (艺人头像), </br>&nbsp;&nbsp;&nbsp;&nbsp;nationality: 大陆(国籍), </br>&nbsp;&nbsp;&nbsp;&nbsp;appImage: htt://aaa.png (众筹APP图片)" +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;pcIcon: http://aaa.png (pc端艺人头像), </br>&nbsp;&nbsp;&nbsp;&nbsp;crowdfundingSummary: aaa(众筹的简介), </br>&nbsp;&nbsp;&nbsp;&nbsp;rank: 1(排序),</br>&nbsp;&nbsp;&nbsp;&nbsp;id: 1(众筹id),</br>&nbsp;&nbsp;&nbsp;&nbsp;isFavorite: 0(否收藏该众筹, 0: 否 1: 是),</br>&nbsp;&nbsp;&nbsp;&nbsp;status: 最热众筹(状态, 最热众筹/最新众筹/即将上线)" +
            "</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}")
    public ResponseParameter getHomeCrowdfunding(@ApiParam(value = "(必填)类型, APP", required = true) @RequestHeader String type,
                                                 @ApiParam(value = "用户token") @RequestHeader(required = false) String token) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取首页的众筹最热最新即将=type:{}  token:{}", type, token);
        if (!CommonValidator.isStringsNotNull(type)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        Integer userId = usrService.getUserId(type, token, false);
        List<Map<String, Object>> list = cfiService.getHomeCrowdfunding(userId);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        logger.info("获取首页的众筹最热最新即将= {}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    @RequestMapping(value = "/getHomeRecommendedArtist/{count}", method = RequestMethod.GET)
    @ApiOperation(value = "(APP/WEB)获取首页的推荐艺人", notes = "返回参数</br>成功:</br>{status:SUCCESS, </br>message:获取成功,</br>data:{</br>&nbsp;&nbsp;info:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;nameDisplay: 张靓颖(艺人展示名),</br>&nbsp;&nbsp;&nbsp;&nbsp;headPc: http://aaa.png (艺人头像)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;headApp: http://aaa.png (APP端头像),</br>&nbsp;&nbsp;&nbsp;&nbsp;artistId: 1(艺人ID),</br>&nbsp;&nbsp;&nbsp;&nbsp;style: [{id: 1(风格ID), typeName: 流行(类型名称)}] 艺人风格</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status:FAILURE, message:参数不全}")
    public ResponseParameter getHomeRecommendedArtist(@ApiParam(value = "(APP/WEB必填)类型, APP/WEB", required = true) @RequestHeader String type,
                                                      @ApiParam(value = "(APP/WEB必填)返回的条数", required = true) @PathVariable Integer count) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取首页的推荐艺人=type:{}  count:{} ", type, count);
        if (!CommonValidator.isStringsNotNull(type)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        List<Map<String, Object>> list = bciService.getHomeRecommendedArtist(count);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("info", list);
        responseParameter.setData(map);
        logger.debug("获取首页的推荐艺人= {}", JSON.toJSON(responseParameter));
        logger.debug("获取首页的推荐艺人= 数量{}", list.size());
        return responseParameter;
    }

}
